package model

import "gin-worked/dao"

type Role struct {
	Id         int    `json:"id"`
	Name       string `gorm:"column(name)" json:"name"`
	Sort       int    `gorm:"column(sort)" json:"sort"`
	Status     int    `gorm:"column(status)" json:"status"`
	IsAdmin    int    `gorm:"column(is_admin)" json:"is_admin"`
	CreateTime string `gorm:"column(create_time)" json:"create_time"`
	UpdateTime string `gorm:"column(update_time)" json:"update_time"`
	DeleteTime string `gorm:"column(delete_time)" json:"delete_time"`
}

func RoleList(data *Role, field []string, order string, offset int, limit int) ([]Role, error) {
	var role []Role
	err := dao.Db.Select(field).Where(data).Offset(offset).Limit(limit).Order(order).Find(&role).Error
	return role, err
}

func RoleTotal(data *Role) int64 {
	var count int64
	dao.Db.Model(&Role{}).Where(data).Count(&count)

	return count
}

func RoleAdd(data *Role) (int, error) {
	err := dao.Db.Create(&data).Error
	return data.Id, err
}

func RoleInfo(data *Role, field []string) (Role, error) {
	var role Role
	err := dao.Db.Select(field).Where(data).First(&role).Error
	return role, err
}

func RoleUpdate(data *Role, content map[string]interface{}) error {
	err := dao.Db.Model(&data).Updates(content).Error
	return err
}
